// components/comImage/comImage.js

const tool = require("../../utils/tool")

Component({

  /**
   * 组件的属性列表
   */
  properties: {
    imageURL: {
      type: String,
      value: ''
    },
    smallSize: {
      type: Number,
      value: 0
    },
    height: {
      type: Number,
      value: 0
    },
  },

  observers: {
    'imageURL'(val) {
      this.changeImg()
    }
  },


  attached() {
    tool.setThemeColor(this)
    this.setData({
      height1: this.properties.height
    })

  },

  /**
   * 组件的初始数据
   */
  data: {
    imageURL: '',
    _this: undefined,
    show: false,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    loadedImg() {
      this.setData({
        show: true
      })
    },
    changeImg() {

      this.setData({
        show:false,
        url: wx.getStorageSync('sysImagURLPrefix') + this.properties.imageURL
      })
      return



      if (wx.getStorageSync('img-' + this.properties.imageURL) == '') {
        this.setData({
          url: wx.getStorageSync('sysImagURLPrefix') + this.properties.imageURL
        })
        wx.downloadFile({
          url: wx.getStorageSync('sysImagURLPrefix') + this.properties.imageURL,
          success: (res) => {
            if (res.statusCode === 200) {
              const tempFilePath = res.tempFilePath;
              const fs = wx.getFileSystemManager();
              fs.readFile({
                filePath: tempFilePath,
                encoding: 'base64',
                success: (data) => {
                  const base64Data = 'data:image/jpeg;base64,' + data.data;
                  if (wx.getStorageSync('img-' + this.properties.imageURL) == '' && wx.getStorageSync('clearCache') != '0') {
                    wx.setStorageSync('img-' + this.properties.imageURL, JSON.stringify({
                      data: base64Data,
                      time: new Date()
                    }))
                  }
                },
                fail: function (err) {

                }
              });
            } else {}
          },
          fail: function (err) {}
        });
      } else {
        let re = JSON.parse(wx.getStorageSync('img-' + this.properties.imageURL))
        re.time = new Date()
        this.setData({
          url: re.data
        })
        wx.setStorageSync('img-' + this.properties.imageURL, JSON.stringify(re))
      }
    }
  }
})